package engine.scene.player
{
	import engine.GameData;
	import engine.data.RoleVo;
	import engine.data.SceneObject;
	import engine.display.Optimize;
	import engine.flag.GameDefine;
	import engine.flag.ObjType;
	import engine.flag.PlayerActionFlag;
	import engine.flag.ResourcesPath;
	import engine.logic.walk.NormalMoveLogic;
	import engine.logic.walk.WalkManager;
	import engine.pool.IResoule;
	import engine.scene.SceneManager;
	import engine.scene.SceneUtil;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.utils.setTimeout;
	
	import newx.com.ResManager;
	import newx.com.control.text.NxLabel;
	import newx.com.loader.SwfLoader;
	import newx.utils.MemoryOptimise;
	
	public class BasePlayerModel extends MoveAction implements IResoule
	{
		protected var _playerPlace 	: Sprite;
		
		/**
		 *	默认模型 
		 */		
		private var _defaultMovie 	: MovieClip;
		
		protected var _nameLabel	: NxLabel;
		
		protected var _familyLabel	: NxLabel;
		
		private var _playerObj		: SceneObject;
		
		private var _playerName	: String;
		
		private var _playerFamilyName	:String;
		
		protected var _moveLogic	: NormalMoveLogic;
		
		private var _loadCompleteFun:Function	=	null;
		
		public function BasePlayerModel()
		{
			_playerPlace	=	new Sprite();
			
			_nameLabel		=	new NxLabel();
			_familyLabel	=	new NxLabel();
			var glow:GlowFilter = new GlowFilter();
			glow.color = 0x000000;
			glow.alpha = 1;
			glow.blurX = 2;
			glow.blurY = 2;
			glow.strength = 5
			glow.quality = BitmapFilterQuality.LOW;
			_nameLabel.txtFilters	=	[glow];
			_familyLabel.txtFilters = 	[glow];
			addChild( _playerPlace );
			addChild( _nameLabel );
			addChild(_familyLabel);
			
			_defaultMovie 	= ResManager.newResClass( "custSkin_86" ) as MovieClip;
		}
		
		public function set LoadCompleteFun(func:Function):void
		{
			_loadCompleteFun	=	func;
		}
		
		public function get playerName():String
		{
			return _playerName;
		}
		
		public function get playerObj():SceneObject
		{
			return _playerObj;
		}
		
		public function set playerObj(value:SceneObject):void
		{
			_playerObj = value;
			
			var modelID	:uint	=	_playerObj.GetProperties("Model");
			
			setTimeout
			( 
				function():void
				{ 
					//加载默认图标custSkin86
					onLoadResComplete( _defaultMovie, null );
					if(_loadCompleteFun != null)
					{
						_loadCompleteFun.call(this);
						_loadCompleteFun =null;
					}
				}, 300 
			);
			var playVisible:Boolean = SceneManager.getInstance().norSceneView.scenePlayerLogic.playerVisible
			if(value is RoleVo ||  value.Type != 1 || playVisible)
			{
				var url:String = ResourcesPath.getRoleModel( _playerObj );
				//				ResManager.poorLoadRes(url,url, onLoadResComplete);
				SwfLoader.StartLoad( url, null, onLoadResComplete );
			}
		}
		
		
		/**
		 * 更新人物模型资源 
		 * @param val
		 * 
		 */		
		public function upDataPlayModel(val:SceneObject):void
		{
			var url:String =ResourcesPath.getRoleModel( val )
			var pList:Array =[];
			if(val is RoleVo)
			{
				pList = [url]
			}
			SwfLoader.StartLoad( url, null, upDataSkin ,pList);
		}
		/**
		 * 换人物模型 
		 * @param movie
		 * @param pList
		 * 
		 */		
		private function upDataSkin( movie:DisplayObject,pList:Array):void
		{
			if( null == movie )return;
			while( _playerPlace.numChildren )
			{
				var mc:DisplayObject = _playerPlace.getChildAt(0) as DisplayObject;
				_playerPlace.removeChild(mc);
				mc = null;
			}
			var sX:int = 1;
			sX = this.direction;
			_playerMovie = movie as MovieClip;
			
			if(_playerMovie.numChildren == 1 && _playerMovie.totalFrames == 1 )
			{
				_playerMovie = _playerMovie.getChildAt( 0 ) as MovieClip;
			}else
			{
				_playerMovie = ResManager.copy( _playerMovie ) as  MovieClip;
			}
			_playerMovie.scaleX = sX;
			_playerPlace.addChild( _playerMovie );
			
			setPlayerName();
			status = _status;
			if(pList!=null && pList[0]!=ResourcesPath.getRoleModel( _playerObj ))
			{
				//主角模型变化太快时 防止先加载的模型后加载完成，主角模型显示问题 
				SwfLoader.StartLoad( ResourcesPath.getRoleModel( _playerObj ), null, upDataSkin );
			}
		}
		//npc加载完成
		private function onLoadResComplete( movie:DisplayObject,pList:Array ):void
		{
			//没有资源
			if( null == movie || _playerObj ==  null)return;
			
			if( movie == _defaultMovie && _playerPlace.numChildren > 0 )
			{
				return;	
			}else
			{
				while( _playerPlace.numChildren )
				{
					_playerPlace.removeChildAt( 0 );
				}
			}
			MovieClip(movie).mouseChildren = MovieClip(movie).mouseEnabled = MovieClip(movie).visible =  
				_playerObj.GetPropertiesEx( "CantVisible" ) != 1;
			
			
			if(!MovieClip(movie).visible )
			{
				return;
			}
			var sX:int = 1;
			sX = this.direction;

			_playerMovie = movie as MovieClip;
			
			if(_playerMovie.numChildren == 1 && _playerMovie.totalFrames == 1 )
			{
				_playerMovie = _playerMovie.getChildAt( 0 ) as MovieClip;
			}else
			{
				_playerMovie = ResManager.copy( _playerMovie ) as  MovieClip;
			}
			_playerMovie.scaleX = sX;
			_playerPlace.addChild( _playerMovie );
			
			if( ObjType.NPC == _playerObj.Type )
			{
				_playerMovie.gotoAndPlay(  PlayerActionFlag.NormalWait );
			}else if( ObjType.Door != _playerObj.Type )
			{
//				玩家状态
				if(_status != PlayerActionFlag.Run)
				_status = _playerObj.GetPropertiesEx( "GameAFKStatus" ) != 1 ? PlayerActionFlag.NormalWait : PlayerActionFlag.Sit;
			}
			
			try
			{
				if( _playerMovie!= _defaultMovie )
				{
					_playerMovie.removeChild( _playerMovie.overhead );
					_playerMovie.removeChild( _playerMovie.chest );
					_playerMovie.removeChild( _playerMovie.origin );
					_playerMovie.removeChild( _playerMovie.ePos );
					_playerMovie.removeChild( _playerMovie.sPos );
				}
			}catch(e:Error){};
			
			addEventListener( Event.ENTER_FRAME, onEnterFrame );
			
			function onEnterFrame( evt : Event ) : void
			{
				removeEventListener( Event.ENTER_FRAME, onEnterFrame );
				
				this.visible	=	false;
				
				updataPlayerInfo( _playerObj );
				
			}
		}
		
		protected function updataPlayerInfo( playerObj : SceneObject):void
		{
			if( null == playerObj ) 
			{
				reset();
				return;
			}
			
			point = getPosPoint( _playerObj );
			
			WalkManager.getInstance().walkLogic.changeDept();
			
			status = _status;
			
			setPlayerName();
		}
		
		public function setPlayerName():void
		{
			_playerName		=	SceneUtil.getName( _playerObj );
			_playerFamilyName=  SceneUtil.getFamilyName(_playerObj);
			_nameLabel.htmlText = _playerName;
			_familyLabel.htmlText = _playerFamilyName;
			if( _playerMovie == null || !_playerMovie.hasOwnProperty("overhead") ) return;
			
			_nameLabel.y = (GameData.SceneType==GameDefine.SceneType_ForbiddenAreaScene)?
				_playerMovie.overhead.y*0.5 - _nameLabel.height:
				_playerMovie.overhead.y - _nameLabel.height ;
			
			_familyLabel.y = (GameData.SceneType==GameDefine.SceneType_ForbiddenAreaScene)
				?_playerMovie.overhead.y*0.5 - _familyLabel.height - _nameLabel.height + 4:
				_playerMovie.overhead.y - _familyLabel.height - _nameLabel.height + 4;
			
			_nameLabel.x	=	_playerMovie.overhead.x - _nameLabel.width * .5;
			_familyLabel.x	=	_playerMovie.overhead.x - _familyLabel.width * .5;
		}
		
		protected function getPosPoint( playerObj : SceneObject ):Point
		{
			return new Point( 0 , 0 );
		}
		
		public function reset():void
		{
			if( null != _playerMovie && _playerMovie != _defaultMovie )
			{
				Optimize.stopMovieClip( _playerMovie );
				if(_playerMovie.parent)
				{
					_playerMovie.parent.removeChild( _playerMovie );
				}
				_playerMovie = null;
			}
			
			if( null != parent )
			{
				parent.removeChild( this );
			}
			
			_playerObj = null;
			
			if( _moveLogic != null )
			{
				_moveLogic.reset();
			}
			
			_playerPlace.filters		=	null;
			
			visible = true;
			
			scaleX	=	scaleY	=	1;
		}
		
		/**
		 *	开始移动  
		 * @param curePoint	当前坐标
		 * @param path		路径
		 * @param es		移动结束时是否改变swf播放状态（0，待机；1,移动状态）
		 */
		public function startMove( path : Array, callBackFunc : Function = null, pList : * = null ):void
		{
			_moveLogic.startMove( this, point, path, callBackFunc, pList );
		}
		
		public function killMove():void
		{
			if( null != _moveLogic )
			{
				_moveLogic.kiss();
			}
		}
		
		/**
		 * 玩家模型容器
		 */
		public function get playerPlace():Sprite
		{
			return _playerPlace;
		}
		
		public function get namePlace():NxLabel
		{
			return _nameLabel;
		}
		
		public function get moveLogic():NormalMoveLogic
		{
			return _moveLogic;
		}
		
		public function set moveLogic(value:NormalMoveLogic):void
		{
			_moveLogic = value;
		}
		public function setVisible( visible : Boolean ):void
		{
			if(this.visible == visible)return;
			if (!_playerObj)return;
			if(_playerObj is RoleVo ||  _playerObj.Type != 1)
			{
				this.visible = true;
				//				return;
			}else
			{
				this.visible = visible;
			}
			if(this.visible)
			{
				upDataPlayModel(_playerObj);
			}
			
			if((this is PlayerModel && !visible))
			{
				MemoryOptimise.stopMovie(this);
			}
		}
	}
}